var showEnergyStatistics,goEnergyDetailsPage;
require(['grid', 'topBar', 'layout', 'date','select2'], function () {
	var postData,rcx,type,orgIds;
	var mapDialog;
	$('#top').topBar({
		left: [{
			type: 'filter-more',
			dateType: 'custom',
			search: function () {
				search();
			},
			item: [
				{
					text: '时间快捷',
					name1: 'beginTime',
					name2: 'endTime',
					type: 'dateLine'
				},
				{
					text: '燃油种类',
					name: 'fuelType',
					width: 100,
					type: 'select',
					data: [
						{value: '柴油', text: '柴油'},
						{value: '汽油', text: '汽油'},
						{value: '电能', text: '电能'},
						{value: 'CNG', text: 'CNG'},
						{value: 'LNG', text: 'LNG'},
						{value: '其他', text: '其他'},
					]
				}
			]
		}
		],
		right: [
			{
				text: '导出',
				type: 'export-btn',
				authority: '',
				item: [
					{
						text: 'Excel导出',
						click: function () {
							exportFile('Excel');
						}
					},
					{
						text: 'PDF导出',
						click: function () {
							exportFile('Pdf');
						}
					}
				]
			}
		]
	});

	//获取左侧选择的节点信息
	function getPostData() {
		rcx = commonFun.getTreeCheckNodeToArgs();
		type = commonFun.getLeftTreeCurrentTreeType();
		orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if (rcx) {
			postData = $.extend(rcx, $("#top").parent().getValues());
			postData.type = type;
			postData.orgIds = orgIds;
		}
		return rcx;
	}

	function exportFile(suffix) {
		if (getPostData()) {
			if ($('#grid').getGridParam("reccount") < 1) {
				$.messager.alert("提示", "请查询数据。", "info");
				return;
			}
			var url = "/energyStatistics/exportEnergyStatistics" + suffix + ".htm";
			$.exportFile(url, postData);
		}
		if ($('#grid').getGridParam("reccount") > 0) {
			if (getPostData()) {
				var url = "/energyStatistics/exportEnergyStatistics" + suffix + ".htm";
				$.exportFile(url, postData);
			}
		}
	}
	function gridInit() {
		$('#grid').grid({
			multiselect: false,
			groupHeader:true,
			url: "/energyStatistics/searchEnergyStatistics.htm",
			colModel: [
				{name: "carId", label: "carId",hidden:true},
				{name: "rcrdDate", label: "数据日期"},
				{name: "lineName", label: "线路名称"},
				{name: "licPltNo", label: "车牌号"},
				{name: "carInCd", label: "车辆编号", width: 80},
				{name: "eqmtCode", label: "设备编码", align: "right"},
				{name: "fuelType", label: "燃料类型"},
				{name: "fuelType1", label: "fuelType1",hidden:true},
				{name: "oper",label: '明细',width:100, title: false,formatter: 'operArea',formatoptions: [{
					text: '明细',
					css: 'operate-edit',
					click: function (data) {
						goEnergyDetailsPage(data.carId, data.rcrdDate,data.fuelType);
					}
				}]
				},
				{name: "trend",label: '趋势图',width:100, title: false,formatter: 'operArea',formatoptions: [{
					text: '趋势图',
					css: 'operate-edit',
					click: function (data) {
						carEnergyAnalysis(data.carId, data.licPltNo,data.rcrdDate,data.fuelType1);
					}
				}]
				},
				{name: "rowCount", label: "记录数", align: "right"},
				{name: "zeroMileCount", label: "里程为0数", align: "right"},
				{name: "zeroEnerCount", label: "能耗为0数", align: "right"},
				{name: "zeroOprtCount", label: "时长为0数", align: "right"},
				{name: "minRcrdTime", label: "最早数据时间"},
				{name: "maxRcrdTime", label: "最晚数据时间"},
				{name: "minTotalMile", label: "最小里程", align: "right"},
				{name: "maxTotalMile", label: "最大里程", align: "right"},
				{name: "maxRngMile", label: "差值里程", align: "right"},
				{name: "sumRngMile", label: "叠加里程", align: "right"},
				{name: "commMaxRngMile", label: "当日区间里程", align: "right"},
				{name: "gpsMile", label: "GPS里程", align: "right"},
				{name: "buMile1", label: "前一日补偿里程", align: "right"},
				{name: "buMile2", label: "后一日补偿里程", align: "right"},
				{name: "commMileBu", label: "最终里程", align: "right"},
				{name: "minTotalEnerCons", label: "最小能耗", align: "right"},
				{name: "maxTotalEnerCons", label: "最大能耗", align: "right"},
				{name: "maxRngEnerCons", label: "差值能耗", align: "right"},
				{name: "sumRngEnerCons", label: "叠加能耗", align: "right"},
				{name: "commMaxRngEnerCons", label: "当日区间能耗", align: "right"},
				{name: "buEne", label: "补偿能耗", align: "right"},
				{name: "commEneBu", label: "最终能耗", align: "right"},
				{name: "minTotalOprtDura", label: "最小运行时长", align: "right"},
				{name: "maxTotalOprtDura", label: "最大运行时长", align: "right"},
				{name: "maxRngOprtDura", label: "差值时长", align: "right"},
				{name: "sumRngOprtDura", label: "叠加时长", align: "right"},
				{name: "commMaxRngOprtDura", label: "当日区间时长", align: "right"}
			]
		});


		$('#grid').grid('setGroupHeaders', {
			useColSpanStyle: true,
			groupHeaders: [
			{
				startColumnName: 'oper',
				numberOfColumns: 2,
				titleText: "<div class='t-header'>操作</div>"
			}, 
		    {
				startColumnName: 'rowCount',
				numberOfColumns: 4,
				titleText: "<div class='t-header'>数量</div>"
			}, {
				startColumnName: 'minRcrdTime',
				numberOfColumns: 2,
				titleText: "<div class='t-header'>时间</div>"
			}, {
				startColumnName: 'minTotalMile',
				numberOfColumns: 9,
				titleText: "<div class='t-header'>里程</div>"
			}, {
				startColumnName: 'minTotalEnerCons',
				numberOfColumns: 7,
				titleText: "<div class='t-header'>能耗</div>"
			}, {
				startColumnName: 'minTotalOprtDura',
				numberOfColumns: 5,
				titleText: "<div class='t-header'>时长</div>"
			}]
		});
	}
	
    function getDialog(option) {
        option = option || {};
        if (mapDialog) {
        	mapDialog.dialog('open');
            if (option.title) {
            	mapDialog.dialog('setOption', 'title', option.title);
            }
            return mapDialog;
        } else {
        	return mapDialog = $('<div>').dialog({
                title: option.title || '能耗明细趋势',
                url: './energy/report/energyStatisticsTrend.htm',
                height: $(window.parent).height()*0.95,
                width:  $(window.parent).width()*0.95,
                autoOpen: true,
                close: function () {
                    $(this).dialog('exec', 'clear');//reset为窗体中的方法
                }
            });
        }
    }
    
	
	//单车能耗趋势图
	function carEnergyAnalysis(carId,licPltNo,rcrdDate,fuelType){
    	 //弹出层打开
    	 getDialog().dialog('exec', 'getCarEnergyDetail', carId, rcrdDate,fuelType);
	}

	function search() {
		if (getPostData()) {
			$('#grid').grid('setGridParam', {
				page: 1,
				datatype: "json",
				postData: postData
			}).trigger("reloadGrid");
		}
	}

	showEnergyStatistics = function (data) {
		//切换成车辆
		commonFun.showLeftTree(commonFun.carTree);
		commonFun.setCheckedNodes(data.carIds);
		$('.topBar').find('[name=beginTime]').val(data.beginTime);
		$('.topBar').find('[name=endTime]').val(data.endTime);
		$('#grid').grid('setGridParam', {
			page: 1,
			datatype: "json",
			postData: data
		}).trigger("reloadGrid");
	}

	//跳转到明细页面
	goEnergyDetailsPage = function (carId, rcrdDate,fuelType) {
		var data = {};
		data.beginTime = rcrdDate;
		data.endTime = rcrdDate;
		data.carId = carId;
		data.fuelType = fuelType;
		$.openTab('qwe2415883a94d72a90530e4ace3931d','showEnergyDetail',[data]);
	};
	$('body').layout();
	//初始化grid
	gridInit();
});